#ifndef _MY_HTTP_
#define _MY_HTTP_
#include <string>
#include <map>

#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
/*
* 用asio库编写的同步获取网页的代码
*/
namespace tool_th{
class myhttp
{
public:
    myhttp();
	~myhttp();

	void init(const std::string &host,const std::string &port,const std::string field,const bool &ishttps,
		const std::map<std::string,std::string> &request_header);
	void init(const std::string &url,const std::map<std::string,std::string> &request_header);
	bool gethtml();

	std::string response_code();
	std::string response_text_decoded_before();
	std::string response_text_decoded();

	void printReuslt(bool output_to_screen =true,
		const std::string &decode_before_file_name = "aaa.txt",
		const std::string &decoded_file_name = "bbb.txt");

public:
	void clear();

	bool read_response_header();
	void read_content();
	void continue_to_read();
	void decompression_data();

	std::string ungzip(const std::string &str);
	std::string gzip(const std::string &str);
	void print_msg(const std::string &str ="",bool need_newline = true);
	void hexdump(const unsigned char *pdata,int len,bool output_to_screen = true,const std::string file_name = "result.txt");

private:
	std::string user_;
	std::string pass_;
	std::string host_;
	std::string port_;
	std::map<std::string,std::string> request_headers_;
	std::string field_;
	bool ishttps_;

private:
	boost::asio::io_service ios_;
	boost::asio::ssl::context ctx_;
	boost::asio::ssl::stream<boost::asio::ip::tcp::socket> *ssl_socket_;

private:
	boost::asio::streambuf recvbuf_;
	std::vector<std::string> response_code_;
	std::map<std::string,std::string> response_headers_;

	bool is_all_received_;//应答包是否已经完整.
	bool isfinished_;//应答过程结束

	std::string response_text_;
	std::string response_temp_;//de-chunked result or 
	std::string response_text_decoded_;

	//以下变量仅用于返回的网页使用chunked编码的情况
	std::string::size_type chunked_index_;



};
}
#endif//_MY_HTTP_